<template>
  <view class="baseForm">
    <!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
	<!-- 条码录入 -->
	
    <u--form labelPosition="left" :model="umodel" ref="uForm" class="form" labelWidth="200rpx"
             errorType="toast">
			     <view class="upload-scan">
			         <scan-bar id="scanInput" ref="storageUnitScanBarRef" placeholder="扫描包装条码" @code-scanned="getStorageUnitByCode" :key="datekey"></scan-bar>
			     </view>
			 <u-form-item label="存货编码" prop="materialCode"  >
			   <u--input v-model="umodel.materialCode" disabled inputAlign="right" clearable
			             border="none" placeholder="存货编码"></u--input>
			 </u-form-item>
			 <u-form-item label="物料名称" prop="materialName"  class="disabled">
			   <u--input v-model="umodel.materialName" disabled inputAlign="right" clearable
			             border="none" placeholder="物料名称"></u--input>
			 </u-form-item>
			 <u-form-item label="数量" prop="totalQuantity" class="disabled">
			   <u--input v-model="umodel.totalQuantity" disabled inputAlign="right" clearable
			             border="none" placeholder="数量"></u--input>
			 </u-form-item>
			 <u-form-item label="单位" prop="quantityUnit" class="disabled">
			   <u--input v-model="umodel.quantityUnit" disabled inputAlign="right" clearable
			             border="none" placeholder="单位"></u--input>
			 </u-form-item>
			 <u-form-item label="供应批次" prop="supplyBatch"  class="disabled">
			   <u--input v-model="umodel.supplyBatch" disabled inputAlign="right" clearable
			             border="none" placeholder="供应批次"></u--input>
			 </u-form-item>
			  <u-line></u-line>
			      <view class="upload-scan">
			          <scan-bar id="scanInput" ref="scanBarRef" placeholder="扫描目标货位" @code-scanned="getBin" :key="datekey1"></scan-bar>
			      </view>
			  <u-form-item label="目标货位号" prop="dstbinCode"  class="disabled">
			    <u--input v-model="umodel.dstbinCode" disabled inputAlign="right" clearable
			              border="none" placeholder="目标货位号"></u--input>
			  </u-form-item>
			  <u-form-item label="目标存储区" prop="dststorageSectionCode"  class="disabled">
			    <u--input v-model="umodel.dststorageSectionCode" disabled inputAlign="right" clearable
			              border="none" placeholder="目标存储区号"></u--input>
			  </u-form-item>                   
    </u--form>
    <!-- 提交按钮 -->
    <button type="primary" @click="submit" class="submit_btn" :loading="posting">提 交</button>
  </view>
</template>
<script lang="ts" setup>
import {reactive, ref} from 'vue';
import ScanBar from '@/components/scan-bar/index.vue';
import {http} from "../../../utils/http";
import ULine from "../../../uni_modules/uview-plus/components/u-line/u-line.vue";
	import { onLoad } from '@dcloudio/uni-app';
// 表单数据定义
const umodel = reactive({
  id: '',
  storageUnitCode: '',
  dststorageTypeId: '',
  dststorageTypeCode: '',
  dststorageTypeName: '',
  dststorageSectionId: '',
  dststorageSectionCode: '',
  dstbinId: '',
  dstbinCode: '',
  storageTypeId: '',
  storageTypeCode: '',
  storageTypeName: '',
  storageSectionId: '',
  storageSectionCode: '',
  binId: '',
  binCode: '',
  materialCode: '',
  warehouseCode: '',
  dstwarehouseId: '',
  dstwarehouseCode: '',
  dstwarehouseName: '',
  materialId: '',
  materialName: '',
  totalQuantity: '',
  quantityUnit: '',
  supplyBatch: '',
  organizationId: '',
  organizationCode: '',
  organizationName: '',
  stockBatch: '',
  plantId: '',
  plantCode: '',
  plantName: '',
  extenalCode: '',
});

// 重置表单信息 & 光标自动blur
const storageUnitScanBarRef = ref();
const scanBarRef = ref();
const datekey = ref(new Date());
const datekey1 = ref(new Date());
let storageSections = reactive([[]]);
let bins = reactive([[]]);
	onLoad(() => {
		setTimeout(()=>{
			storageUnitScanBarRef.value.reset();
		}, 1000);
		
	})
async function getStorageUnitByCode(result: any) {
	storageUnitScanBarRef.value.reset();
	datekey.value= new Date();
	if (result) {
		try {
  umodel.extenalCode = result;
  const data = await http({
    url: '/wms/storageunit/getin/' + umodel.extenalCode,
    method: 'GET',
    data: {}
  });
  var info = {
	id: data.data.id,
	warehouseCode: data.data.warehouseCode,
	warehouseId: data.data.warehouseId,
	 materialId: data.data.materialId,
    materialCode: data.data.materialCode,
	materialName: data.data.materialName,
    totalQuantity: data.data.totalQuantity,
	quantityUnit: data.data.quantityUnit,
    supplyBatch: data.data.supplyBatch,
	organizationId: data.data.organizationId,
	organizationCode: data.data.organizationCode,
	organizationName: data.data.organizationName,
	stockBatch: data.data.stockBatch,
	plantId: data.data.plantId,
	plantCode: data.data.plantCode,
	plantName: data.data.plantName,
	storageTypeId: data.data.storageTypeId,
	storageTypeCode: data.data.storageTypeCode,
	storageTypeName: data.data.storageTypeName,
	storageSectionId: data.data.storageSectionId,
	storageSectionCode: data.data.storageSectionCode,
	storageUnitCode:data.data.storageUnitCode,
	binId: data.data.binId,
	binCode: data.data.binCode
	
  }
  if(data.data.isCheck==1) {
  	uni.$u.toast('质检未完成！');
	 storageUnitScanBarRef.value.reset();
	 datekey.value= new Date();
  	return;
  }
  Object.assign(umodel, info);
  
    scanBarRef.value.reset();
		datekey1.value= new Date();
  } catch (error) {
  	console.error("111");
	storageUnitScanBarRef.value.reset();
	datekey.value= new Date();
  }
  }
  else{
	  umodel.extenalCode = '';
	  storageUnitScanBarRef.value.reset();
	  datekey.value= new Date();
  }
}

async function getBin(result: any) {
	scanBarRef.value.reset();
	datekey1.value= new Date();
	if (result) {
  umodel.dstbinCode = result;
  const data = await http({
    url: `/wms/bin/getBinByCode/${umodel.dstbinCode}`,
    method: 'GET',
    data: {}
  });
  var info = {
	dstbinId:data.data.id,
	dstbinCode:data.data.binCode,
	dststorageSectionId: data.data.storageSectionId,
  	dststorageSectionCode: data.data.storageSectionCode,
	dststorageTypeId: data.data.storageTypeId,
	dststorageTypeCode: data.data.storageTypeCode,
	dststorageTypeName: data.data.storageTypeName,
	dstwarehouseId: data.data.warehouseId,
	dstwarehouseCode: data.data.warehouseCode,
	dstwarehouseName: data.data.warehouseName,
  }
  Object.assign(umodel, info);
  }
  else{
  	  umodel.dstbinCode = '';	  
  }
}

// 校验提交
const uForm = ref();
const posting = ref(false);


async function submit() {
	if (!umodel.extenalCode
			|| !umodel.materialCode
			|| !umodel.materialName
			|| !umodel.materialId) {
		uni.$u.toast('请先扫描包装条码！');
		  storageUnitScanBarRef.value.reset();
	} else {
		if (!umodel.dstbinCode) {
			uni.$u.toast('请先扫描目标货位！');
			  scanBarRef.value.reset();
			return;
		}
		else {
			if (umodel.binCode == umodel.dstbinCode) {
				uni.$u.toast('目标货位和源货位相同！');
				  scanBarRef.value.reset();
				return;
			}
		const data = await http({
		  url: '/wms/warehousemove/movematerial',
		  method: 'POST',
		  data: umodel
		});
		uni.$u.toast('转储成功');
			uForm.value.resetFields();
			scanBarRef.value.reset();
            storageUnitScanBarRef.value.reset();
	}
	}
}

</script>

<style lang="scss">
	.upload-scan {
			padding: 10px 40rpx 10px 40rpx;
		}
.baseForm {
	.form {
		padding: $uni-page-padding;
	}

  .u-form {
    margin-top: 5px;

    .scan-bar {
      display: flex;
      flex: 1;
    }
  }

  .u-line {
    padding: 10px 0;
  }

  .search {
    display: flex;

    .custom-style {
      // width: 120px;
      margin: 10px 5px 5px;
      height: 36px;
      border-radius: 8px;

      &:nth-child(1) {
        flex: 1
      }

      &:nth-child(2) {
        flex: 3
      }
    }
  }
.submit_btn {
		margin: 60rpx;
		border-radius: 60rpx;
	}
  .list {
    .list-title {
      line-height: 40px;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.4);
    }

    .card {
      margin-top: 5px;
      margin-bottom: 10px;
      border-radius: 7px;
      display: flex;
      align-items: center;
      padding: 10px 0;
      transition: all 300ms ease;
      box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

      .left {
        // flex-basis: 90rpx;
        padding-right: 10px;
        text-align: right;
        font-size: 13px;

        .enter {
          line-height: 20px;
          margin-bottom: 5px;
        }

        .number {
          font-size: 13px;
          line-height: 18px;
        }

        :deep(.u-icon--right) {
          flex-direction: row-reverse;
        }
      }

      .right {
        flex: 1;
        padding-left: 15px;

        .title {
          font-size: 15px;
          line-height: 20px;
          margin-bottom: 5px;
        }

        .body {
          color: rgba(0, 0, 0, 0.4);
          font-size: 13px;
          line-height: 18px;
        }
      }
    }
  }
}

</style>
